var $;
var form;
var upload;
var element;
var layer;
//正则表达式判断字母数字同时存在并不包含特殊字符
var reg =/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/;

layui.use(['form','upload', 'element', 'layer'], function(){
    form = layui.form;
    $ = layui.jquery;
    upload = layui.upload;
    element = layui.element;
    layer = layui.layer;

    var create =localStorage.getItem("user");
    var founder=JSON.parse(create);//将获取json字符串转json对象
    $("input[name=account]").val(founder.account);
    $("input[name=userId]").val(founder.user_id);
    $("input[name=email]").val(founder.email);
    $("#avatarUrl").attr("src",founder.past1+founder.avatar_url);
    var id=$("input[name=userId]").val();

    //监听提交
    $("#sub").bind('click',function(){
        var conpwd=$("input[name=confirmpassword]").val();
        var newpwd=$("input[name=newpassword]").val();

        var create=localStorage.getItem("user");
        create=JSON.parse(create);//将获取json字符串转json对象
        userId=create.user_id;
        if(newpwd!=''&&conpwd!=''){
            $.ajax({
                url: '/account/getPassword',
                type: "post",
                dataType: "json",
                data: {"id": userId,"conpwd":conpwd},
                success: function (data) {
                    if(data==false){
                        var conpwd=$("input[name=confirmpassword]").val();
                        var newpwd=$("input[name=newpassword]").val();
                        if(conpwd==newpwd){
                            if (!reg.exec(conpwd)) {
                                layer.msg('密码过于简单', {icon: 5});
                            }else{
                                updUserPwd(conpwd);
                            }
                        }else{
                            layer.msg('两次密码不一致', {icon: 2});
                        }

                    }else{
                        layer.msg('不能与原密码相同', {icon: 5});
                    }
                }
            })
        }else{
            layer.msg('必填项不能为空', {icon: 2});
        }
    });

    //修改密码
    function updUserPwd(conpwd){
        var create=localStorage.getItem("user");
        create=JSON.parse(create);//将获取json字符串转json对象
        userId=create.user_id;
        $.ajax({
            url : '/account/updUserPwd',
            type : "post",
            dataType : "json",
            data : {"id":userId,"newpwd":conpwd},
            success : function(data) {
                if(data){
                    /*layer.confirm('修改成功', {
                        btn: ['确定'] //按钮
                    }, function(){
                        location.href="/login/SyLogin.html";
                    });*/
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        area: ['300px', '200px'],
                        closeBtn: 0, //不显示关闭按钮
                        anim: 2,
                        icon:1,
                        shadeClose: true, //开启遮罩关闭
                        content: '<div style="font-size: 15px; text-align: center; line-height: 80px;">' +
                            '<i class="layui-icon layui-icon-face-smile" style="font-size: 18px; color: #009688;"></i> 修改成功</div>',
                        btn: ['确定'],  //按钮
                        yes: function(){
                            location.href="/login/SyLogin.html";
                        }
                    });
                }
            }
        })
    }

    var uploadInst = upload.render({
        elem: '#upload_pictures'
        ,url: '/account/updAvatarUrl'
        ,data: {id:id} //可选项。额外的参数，如：{id: 123, abc: 'xxx'}
        ,accept:'images' //允许上传的文件
        ,acceptMime: 'image/jpeg,image/png' //文件筛选
        ,ext: 'jpg|png|bmp|jpeg' //允许上传的文件后缀
        ,size: 4096 //限制文件大小单位 KB
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){

                $('#avatarUrl').attr('src', result);

                //导航栏头像刷新
                parent.$(".layui-nav-img").replaceWith("<img class='layui-nav-img' src='"+result+"'>");
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code != 0){
                 layer.msg('上传失败');
            }
        },error: function(){
        }

    });
    load();
});
//按钮点击事件
function load() {
    $(".tit_refresh").bind('click', function () {
        top.document.location.reload();
        LoginTitle();
    });
}

